package com.tjetc;

import com.tjetc.vo.Result;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ServiceMain {
    public static void main(String[] args) {
        try {
            //创建一个socketService
            ServerSocket serverSocket=new ServerSocket(8888);
            ExecutorService pool = Executors.newCachedThreadPool();
            while (true){
                Socket socket = serverSocket.accept();
                pool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                            ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                            //获取接口名称
                            String interfaceName = in.readUTF();
                            //获取方法名
                            String methodName = in.readUTF();
                            //获取参数类别
                            Class[] paramType = (Class[])in.readObject();
                            //获取参数
                            Object[] args = (Object[]) in.readObject();
                            //参数
                            Properties properties=new Properties();
                            properties.load(ServiceMain.class.getClassLoader().getResourceAsStream("service.properties"));
                            String interfaceImpl=properties.getProperty(interfaceName);
                            //开始映射
                            Class<?> interfaceClass = Class.forName(interfaceImpl);
                            //获取构造方法
                            Constructor<?> constructor = interfaceClass.getDeclaredConstructor();
                            Object o = constructor.newInstance();
                            //获取方法
                            Method method=interfaceClass.getDeclaredMethod(methodName,paramType);
                            Result result=(Result) method.invoke(o,args);
                            out.writeObject(result);
                            out.close();
                            in.close();
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        } catch (InstantiationException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
